package priv.yiyuan.example.design.proxy;

import java.util.Date;

/**
 * @author YiYuan
 * @data 2025/9/25
 * @apoNote
 */
public class SecurityAuditPaymentProxy implements PaymentService {

    private RealPaymentService realPaymentService;

    private String currentUserId;

    public SecurityAuditPaymentProxy(RealPaymentService realPaymentService, String currentUserId) {
        this.realPaymentService = realPaymentService;
        this.currentUserId = currentUserId;
    }

    private boolean preAudit(double amount, PaymentStrategy strategy) {
        System.out.println("\n[安全审计代理] 前置审计: 用户 " + currentUserId + " 尝试支付 " + amount + " 元, 使用策略: " + strategy.getClass().getSimpleName() + " 在 " + new Date());

        if ("blockedUser".equals(currentUserId)) {
            System.out.println("[安全审计代理] 审计失败: 用户 " + currentUserId + "被阻止支付。");
            return false;
        }

        System.out.println("[安全审计代理] 前置审计通过。");
        return true;
    }

    private void postAudit(double amount, PaymentStrategy strategy) {
        System.out.println("[安全审计代理] 后置审计: 用户 " + currentUserId + " 支付 " + amount + " 元操作完成, 使用策略: " + strategy.getClass().getSimpleName() + "..");
    }

    @Override
    public void processPayment(double amount, PaymentStrategy strategy) {
        if (this.preAudit(amount, strategy)) {
            realPaymentService.processPayment(amount, strategy);
            this.postAudit(amount, strategy);
        }
        else {
            System.out.println("[安全审计代理] 支付请求被拒绝。");
        }
    }
}
